#include "mycoin.h"
#include <QDebug>
MyCoin::MyCoin(QString btnImg)
{
    QPixmap pix;
    bool ret=pix.load(btnImg);
    if(!ret)
    {
        QString str=QString("图片%1加载失败").arg(btnImg);
        qDebug()<<str;
        return;
    }
    this->setFixedSize(pix.width(), pix.height());//设置图片大小
    this->setStyleSheet("QPushButton{border:Opx;}");//设置不规则图片样式
    this->setIcon(pix);//设置图标
    this->setIconSize(QSize(pix.width(), pix.height()));//设置图标大小
    timer1=new QTimer(this);
    timer2=new QTimer(this);//初始化定时器对象
    connect(timer1,&QTimer::timeout,[=]()
           {
               QPixmap pix;
               QString str=QString(":/res/0%1").arg(this->min++);
               pix.load(str);
               this->setFixedSize(pix.width(), pix.height());//设置图片大小
               this->setStyleSheet("QPushButton{border:Opx;}");//设置不规则图片样式
               this->setIcon(pix);//设置图标
               this->setIconSize(QSize(pix.width(), pix.height()));//设置图标大小
               if(this->min>this->max)
                   {
                   this->min=1;
                   isAnimation=false;
                   timer1->stop();
               }
           });//监听正面翻反面的信号，并且翻转金币
    connect(timer2,&QTimer::timeout,[=]()
            {
                QPixmap pix;
                QString str=QString(":/res/0%1").arg(this->max--);
                pix.load(str);
                this->setFixedSize(pix.width(), pix.height());//设置图片大小
                this->setStyleSheet("QPushButton{border:Opx;}");//设置不规则图片样式
                this->setIcon(pix);//设置图标
                this->setIconSize(QSize(pix.width(), pix.height()));//设置图标大小
                if(this->max<this->min)
                {
                    this->max=8;
                    isAnimation=false;
                    timer2->stop();
                }
            });//监听反面翻正面的信号，并且翻转金币
}
void MyCoin::mousePressEvent(QMouseEvent* e)
{
    if(this->isAnimation)
        return;
    else if(this->isWin)
        return;
    else
    {
        QPushButton::mousePressEvent(e);
    }
}
void MyCoin::changeFlag()
{
    if(this->flag)
    {
        timer1->start(25);
        this->flag=false;
    }
    else
    {
        timer2->start(25);
        this->flag=true;
    }
}//改变正反面

